package com.pe.rentcar.dao.jdbc;


import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.pe.rentcar.Marca;
import com.pe.rentcar.dao.MarcaDao;


@Repository
public class MarcaDaoJdbc extends SimpleJdbcDaoSupport implements MarcaDao {
	
	private Logger logger = Logger.getLogger(MarcaDaoJdbc.class);

	@Autowired
	public MarcaDaoJdbc(DataSource dataSource) {
		setDataSource(dataSource);
	}
	
	
	@Override
	public Marca find(Integer id) {
		Marca marca = null;
		try{
			marca =  getSimpleJdbcTemplate().queryForObject(
					"select id, name from marca where id=?",
							new BeanPropertyRowMapper<Marca>(Marca.class), id);
		}catch(EmptyResultDataAccessException ex){
			logger.debug("Records not found for id " + id);
		}
		return marca;
	}
	
	@Override
	public List<Marca> findAll() {
		return getSimpleJdbcTemplate().query(
				"select id, name from marca",
				new BeanPropertyRowMapper<Marca>(Marca.class));
	}

	
}
